package com.controller.admin;

import com.domain.BlogType;
import com.domain.PageBean;
import com.github.pagehelper.Page;
import com.service.BlogService;
import com.service.BlogTypeService;
import com.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController {
    @Resource
    private BlogTypeService blogTypeService;
    @Resource
    private BlogService blogService;
    @RequestMapping("/save.do")
    public String save(BlogType blogType, HttpServletResponse response) throws IOException {
        int res=0;
        if(blogType.getId()==null){
            res=blogTypeService.add(blogType).intValue();
        }else {
            res=blogTypeService.update(blogType).intValue();
        }
        JSONObject result=new JSONObject();
        if(res>0){
            result.put("success",Boolean.valueOf(true));
        }else {
            result.put("success",Boolean.valueOf(false));
        }
        ResponseUtil.write(response,result);
        return null;
    }
    @RequestMapping("/delete.do")
    public String delete(@RequestParam("ids") String ids,HttpServletResponse response) throws IOException {
        String[] idsStr=ids.split(",");
        JSONObject res=new JSONObject();
        for (int i = 0; i <idsStr.length ; i++) {
            int sum=blogService.getBlogByTypeId(Integer.valueOf(idsStr[i]));
                    if(sum>0){
                        res.put("exist","博客类别下有博客，不能删除");
                    }else {
                        blogTypeService.delete(Integer.valueOf(idsStr[i]));
                    }

        }
        res.put("success",Boolean.valueOf(true));
        ResponseUtil.write(response,res);
        return  null;
    }
    @RequestMapping(value = "/list.do",produces = "text/json;charset=utf-8")
    public String list(@RequestParam(value = "page",required = false)String page,
                       @RequestParam(value ="rows",required = false)String rows,
                       HttpServletResponse response) throws IOException {
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("start",pageBean.getStart());
        map.put("size",pageBean.getPageSize());
        //查询博客类型列表
        List<BlogType> blogTypeList=blogTypeService.list(map);
        Long total=blogTypeService.getTotal(map);
        JSONObject res=new JSONObject();
        JSONArray jsonArray=JSONArray.fromObject(blogTypeList);
        res.put("rows",jsonArray);
        res.put("total",total);
        ResponseUtil.write(response,res);
        return null;
    }


}
